//
//  AppDelegate.swift
//  Sina
//
//  Created by YYY on 2017/7/24.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

import SVProgressHUD
import AFNetworking
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = CPJMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        // Override point for customization after application launch.
        return true
    }
}


extension AppDelegate
{
    //设置应用程序的额外信息
    func setupAdditionalInfo()
    {
        //设置最小的解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //设置用户授权的通知
        //使用#availabel检测设备版本，获取用户授权，显示应用badgeNumber
        if #available(iOS 10.0, *)
        {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        }
        else
        {
            let notificationSettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notificationSettings)
            // Fallback on earlier versions
        }
    }
}
extension AppDelegate
{
    //加载app信息
    fileprivate func loadAppInfo()
    {
        //模拟异步加载数据
        DispatchQueue.global().async {
            
//            let url = Bundle.main.url(forResource: "main", withExtension: "json")
//            let data = NSData(contentsOf: url!)
//            guard let resource = Bundle.main.path(forResource: "main", ofType: "json"),
//                let data = NSData(contentsOfFile: resource) else
//            {
//                return
//            }
          
            guard let url = Bundle.main.url(forResource: "main", withExtension: "json"),
            let data = NSData(contentsOf: url) else
            {
                return
            }

            //沙盒路径
            //let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let endPath = (kDocumentPath as NSString) .appendingPathComponent("main.json")
            //写入磁盘
            data.write(toFile: endPath, atomically: true)
        }
    }
}

